var Grandsys =  function(){
var queuetype = "9"; // Queue Types: Station=3, User=9, Wrkgrp=10, Line=15
var isConnect =false;
return {
    Ver:"1.0.0.1",
    Softphone:'Grandsys Web Softphone',
    EICConn:'',
    WatcherAdapter :'',
    Queue:'',
    User:'',  
	System:'CIC',
    Connect:function(loginobj,evt){
    		  var ret = -999;
              if(isConnect==false)
              {
				  
				  var ConnectObj= ConnectAdapter(this,this.System);
				
				  ret = ConnectObj.Connect(loginobj,evt);
                  
                  //this.evt=evt.CallLists;
                }
                
                isConnect=true;	
              return   ret;
        
       }
}    
}();

function clsUser()
{
    return UserAdapter(Grandsys.System);
}

function Callobj()
{

    return{
          
          Id: '',
          /*Alerting = 1

            Connected = 105

            Dialing = 103

            Disconnected = 106

            Initializing = 100

            Manual Dialing = 102

            Offering = 101

            On Hold = 4

            Proceeding = 104

            Station Audio = 107
            */
          Status: '',
          
          StatusName:''
        
        };
    
}




function clsQueueEvent()
{   
    return QueueEventAdapter(Grandsys.System);
    
}


function loginobj()
{

    return{
              
          App: Grandsys.Softphone,
          Server: '',
          User:'',
          Password:'',
          Station:''
          
        };
    
}


function clsCallList()
{
    var callList = new Array();  
    
    
    /**
     * Checks whether or not the specified object exists in the array.
     * @param {Object} o The object to check for
     * @return {Number} The index of o in the array (or -1 if it is not found)
     */
    function indexOf(o){
       for (var i = 0, len = callList.length; i < len; i++){
          if(callList[i].Id == o) return i;
       }
       return -1;
    };
    
    return{              
            Add:function(Callobj){
                var index =indexOf(Callobj.Id);
                if(index==-1)
                {
                    callList[callList.length]=Callobj;
                }
                else
                {
                    callList[index]=Callobj;
                }
                return callList;
//                var text="";
//                for(x in callList) 
//                {
//                  
//                  text = text + callList[ x ].Id + callList[ x ].StatusName +"\n" 
//                }
//                dataCallList.innerText = text;
            }, 
            Remove : function(o){
               var index = indexOf(o);
               if(index != -1){
                   callList.splice(index, 1);
               }
               
               return callList;
            },
            Change : function(Callobj){
                var index =indexOf(Callobj.Id);
                if(index==-1)
                {
                    //callList[callList.length]=Callobj;
                }
                else
                {
                    callList[index]=Callobj;
                }
                return callList;
//                var text="";
//                for(x in callList) 
//                {
//                  
//                  text = text + callList[ x ].Id + callList[ x ].StatusName +"\n" 
//                }
//                dataCallList.innerText = text;
            },
            GetCallObj:function(id)
            {
                var index =indexOf(id);
                if(index==-1)
                {
                    return null;
                }
                else
                {
                    return callList[index];
                }
            },
			GetNextActiveCall:function()
			{
				var xcall;
				var retid=0;
				for(xcall in list) 
				{				  
					if(list[ xcall ].State!="106")
					{
						retid = list[ xcall ].Id;
						break;
					}
				}

				return retid;
			},
            GetList:function(){return callList;}
          
        };
    
}



function clsCallTool()
{
    return CallToolAdapter(Grandsys.System);
}



function clsButtonBar()
{
    var callid = 0 ;
    var transfercallid = 0 ;
    var conferencecallid = 0 ;
    
    //������ 
    var _txtNumber={};
    
    //�绰����
    var _btnpickup={};
    var _btndisconnect={};
    var _btnhold={};
    var _btnmute={};
    var _btndail={};
    var _btnblindtransfer={};
    var _btnmaketransfer={};
    var _btnconsulttransfer={};
    var _btncanceltransfer={};
    var _btnmakeconference={};
    var _btnconsultconference={};
    var _btncancelconference={};
    
    
    //agent ��·״̬
    
    
    var _mode; //0 ��״̬ //1: ����ת��; // 2������� //3 ������
    
    
    
    function setbar(statuscode,cid)
            {
                //alert(statuscode);
                /*Alerting = 1

                Connected = 105

                Dialing = 103

                Disconnected = 106

                Initializing = 100

                Manual Dialing = 102

                Offering = 101

                On Hold = 4

                Proceeding = 104

                Station Audio = 107
                */
                if(callid == 0) 
                    callid = cid;
                if(callid == cid)
                {
                    switch (statuscode)
                    {
                        case 0 :
                            {
                                try
                                {
                                    _btnpickup.disabled =true;
                                    _btndisconnect.disabled=true;
                                    _btnhold.disabled=true;
                                    _btnmute.disabled = true;
                                    _btnhold.value="hold";
                                    _btnblindtransfer.disabled = true;
                                    _btnmaketransfer.disabled = true;
                                    _btnconsulttransfer.disabled = true;
                                    _btncanceltransfer.disabled = true;
                                    _btnmakeconference.disabled = true;
                                    _btnconsultconference.disabled = true;
                                    _btncancelconference.disabled = true;
                                    _btndail.disabled = false;
                                   callid = 0;
                                }
                                catch(e)
                                {
                                    
                                }
                                break;
                            }
                        case 1 :
                            {
                                try
                                {
                                    _btnpickup.disabled =false;
                                    _btndisconnect.disabled=false;
                                   
                                }
                                catch(e)
                                {
                                
                                }
                                break;
                            }
                        case 4 :
                            {
                                try
                                {
                                   
                                    _btnhold.value="unhold";
                                   
                                }
                                catch(e)
                                {
                                
                                }
                                break;
                            }
                        case 100 :
                            {
                                try
                                {
                                    //_btnhold.disabled =false;
                                    _btndisconnect.disabled=false;
                                    
                                    if(_mode==1)
                                    {
                                        //����ȡ��ת��
                                        _btnblindtransfer.disabled = true;
                                        _btnmaketransfer.disabled = true;
                                        _btncanceltransfer.disabled = false;
                                        _btndisconnect.disabled=true;
                                        _btnhold.disabled=true;
                                        _btnmute.disabled = true;
                                        
                                        _btnmakeconference.disabled = true;
                                    }
                                    
                                    if(_mode==2)
                                    {
                                        //����ȡ�����
                                        _btnmakeconference.disabled = true;
                                        _btnconsultconference.disabled = true;
                                        _btncancelconference.disabled = false;
                                        _btndisconnect.disabled=true;
                                        _btnhold.disabled=true;
                                        _btnmute.disabled = true;
                                        
                                        _btnmaketransfer.disabled = true;
                                        _btnblindtransfer.disabled = true;
                                    }
                                }
                                catch(e)
                                {
                                
                                }
                                break;
                            }
                        case 101 :
                            {
                                try
                                {
                                    _btnpickup.disabled =false;
                                    _btndisconnect.disabled=false;
                                }
                                catch(e)
                                {
                                
                                }
                                break;
                            }
                        case 103 :
                            {
                                try
                                {
                                    //_btnhold.disabled =false;
                                    _btndisconnect.disabled=false;
                                    
                                    if(_mode==1)
                                    {
                                        //����ȡ��ת��
                                        _btnblindtransfer.disabled = true;
                                        _btnmaketransfer.disabled = true;
                                        _btncanceltransfer.disabled = false;
                                        _btndisconnect.disabled=true;
                                        _btnhold.disabled=true;
                                        _btnmute.disabled = true;
                                    }
                                    
                                    if(_mode==1)
                                    {
                                        //����ȡ�����
                                        _btnmakeconference.disabled = true;
                                        _btnconsultconference.disabled = true;
                                        _btncancelconference.disabled = false;
                                        _btndisconnect.disabled=true;
                                        _btnhold.disabled=true;
                                        _btnmute.disabled = true;
                                    }
                                    
                                    
                                    
                                }
                                catch(e)
                                {
                                
                                }
                                break;
                            }
                        case 105 :
                            {
                                try
                                {
                                    _btnpickup.disabled =true;
                                    _btndail.disabled =true;
                                    _btnhold.disabled = false;
                                    _btnmute.disabled = false;
                                    _btndisconnect.disabled=false;
                                    _btnhold.value="hold";
                                    _btnblindtransfer.disabled = false;
                                    _btnmaketransfer.disabled = false;
                                    _btnmakeconference.disabled = false;
                                    //alert(callid);
                                    //��ת��״̬
                                    _btnconsulttransfer.disabled = true;
                                    _btncanceltransfer.disabled = true;
                                    _btnconsultconference.disabled = true;
                                    _btncancelconference.disabled = true;
                                    
                                    if(_mode==1)
                                    {
                                        //����ת��
                                        _btnpickup.disabled =true;
                                        _btnblindtransfer.disabled = true;
                                        _btndisconnect.disabled = true;
                                        _btnmaketransfer.disabled = true;
                                        _btnmakeconference.disabled = true;
                                        _btnconsulttransfer.disabled = false;
                                        _btncanceltransfer.disabled = false;
                                    }
                                    
                                    if(_mode==2)
                                    {
                                        //����ת��
                                        _btnpickup.disabled =true;
                                        _btnblindtransfer.disabled = true;
                                        _btndisconnect.disabled = true;
                                        _btnmakeconference.disabled = true;
                                        _btnmaketransfer.disabled = true;
                                        _btnconsultconference.disabled = false;
                                        _btncancelconference.disabled = false;
                                    }
                                    
                                    
                                    if(_mode==3)
                                    {
                                        //������
                                        
                                        _btnpickup.disabled =true;
                                        _btnblindtransfer.disabled = true;                                        
                                        _btnmakeconference.disabled = true;
                                        _btnmaketransfer.disabled = true;
                                        _btnconsulttransfer.disabled = true;
                                        _btncanceltransfer.disabled = true;
                                        _btnconsultconference.disabled = true;
                                        _btncancelconference.disabled = true;
                                        _btnhold.disabled=true;
                                        
                                    }
                                    
                                    
                                    
                                    
                                    
                                }
                                catch(e)
                                {
                                      // alert(e);
                                
                                }
                                break;
                            }
                        case 106 :
                            {
                                try
                                {
                                    _btnpickup.disabled =true;
                                    _btndisconnect.disabled=true;
                                    _btnhold.disabled=true;
                                    _btnmute.disabled = true;
                                    _btnhold.value="hold";
                                    _btnblindtransfer.disabled = true;
                                    _btnmaketransfer.disabled = true;
                                    _btnconsulttransfer.disabled = true;
                                    _btnmakeconference.disabled = true;
                                    _btnconsultconference.disabled = true;
                                    _btndail.disabled =false;
                                    callid = 0;
                                    if((_mode==3) && (conferencecallid==0 ))
                                    {
                                        //alert("111");
                                        _mode=0;
                                    }
                                    //alert(_mode);
                                    if(_mode==1)
                                    {
                                        //alert("canceltransfer-1");
										CustomEvent('',21007);
                                        canceltransfer();
                                    }
                                    
                                    if(_mode==2)
                                    {
                                        //alert("canceltransfer-2");
										CustomEvent('',21007);
                                        cancelconference();
                                    }
									
									

									

									
                                    
                                    
                                }
                                catch(e)
                                {
                                
                                }
                                
                                break;
                            }

                    }
                    
                    
                    
                }
                
                if(cid==transfercallid)
                    {
                        //alert(transfercallid);
                        if(statuscode==106)
                        {
                            transfercallid=0;
                            _btnconsulttransfer.disabled = true;
                        }    
                        //alert(transfercallid);
                    }
               if( cid==conferencecallid)
                    {
                        //alert(_mode);
                        //alert(transfercallid);
                        if(statuscode==106)
                        {
                            conferencecallid=0;
                            _btnconsultconference.disabled = true;
                        }    
                        //alert(transfercallid);
                    }
                
                
            };
    function canceltransfer()
    {
        //alert(callid);
       var tool = clsCallTool();                      
       tool.Disconnect(callid); 
       callid=transfercallid ; 
       _mode = 0;
       //alert(callid);
        
       if(callid!=0)
       {
           tool.Hold (callid);     
                      
           setbar(105,callid);
       }
       else
       {
           setbar(0,callid);
       }
       
       //alert(statuscode);
    };
    
    
    function cancelconference()
    {
        //alert(callid);
       var tool = clsCallTool();                      
       tool.Disconnect(callid); 
       callid=conferencecallid ; 
       _mode = 0;
       //alert(callid);
        
       if(callid!=0)
       {
           tool.Hold (callid);     
                      
           setbar(105,callid);
       }
       else
       {
           setbar(0,callid);
       }
       
       //alert(statuscode);
    };
    
    
    return{
          CurrentId: function()
          {
            return callid;
          },    
          Pickup: function()
            {
                var tool = clsCallTool();
                
                tool.Pickup(callid);   
                return 0;               
                
            },
          Disconnect: function()
            {
                //alert("11");
                var tool = clsCallTool();                
                tool.Disconnect(callid); 
                return 0;                              
            },
          Hold: function()
            {
                //alert("11");
                var tool = clsCallTool();                
                tool.Hold (callid);                 
                return 0;                              
            },
          Mute: function()
            {
                //alert("11");
                var tool = clsCallTool();                
                tool.Mute (callid);                 
                if(_btnmute.value=="mute")
                {
                    _btnmute.value="unmute";
                }
                else
                {
                    _btnmute.value="mute";
                }
				CustomEvent('',21001);
				Eventtype.setCallid(callid,21001);
                return 0;                              
            },
          Dail: function()
            {
                //alert("11");
                                 
                if(_txtNumber.value=="")
                {
                   
                }
                else
                {
                   var tool = clsCallTool();                
                   tool.Dail (_txtNumber.value);
                }
                 
                return 0;                             
            },
          BlindTransfer: function()
            {
                //alert("11");
                                 
                if(_txtNumber.value=="")
                {
                   
                }
                else
				//alert("轉接號碼....."+_txtNumber.value);
                {
                   // alert(_txtNumber.value)
                   var tool = clsCallTool();                
                   tool.Transfer (_txtNumber.value,callid);
                   setbar(0,callid);
                }
                
                return 0;               
                               
            },
          MakeTransfer: function()
            {
                //alert("11");
                                 
                if(_txtNumber.value=="")
                {
                   
                }
                else
                {
                   // alert(_txtNumber.value)
                   var tool = clsCallTool(); 
                   transfercallid=callid ;              
                   callid=tool.Dail (_txtNumber.value);
                   _mode=1;
				   CustomEvent('',20013);
				   Eventtype.setCallid(callid,20013);
                   //alert(callid);
                   //alert(transfercallid);
                }
                return 0;               
                               
            },
          ConsultTransfer: function()
            {                
                
               
               var tool = clsCallTool();                      
               tool.ConsultTransfer(transfercallid,callid);
               _mode=0;
               //CustomEvent('',20008);
			   CustomEvent('',30001);
                              
               return 0;                               
            },
          CancelTransfer: function()
            {                
                
               canceltransfer();
               
               return 0;                               
            },
          MakeConference: function()
            {
                //alert("11");
                                 
                if(_txtNumber.value=="")
                {
                   
                }
                else
                {
                   // alert(_txtNumber.value)
                   var tool = clsCallTool(); 
                   conferencecallid=callid ;              
                   callid=tool.Dail (_txtNumber.value);
                   _mode=2;
				   CustomEvent('',20013);
				   Eventtype.setCallid(callid,20013);
                   //alert(callid);
                   //alert(transfercallid);
                }
                return 0;                              
            },
          Conference: function()
            {                
                
               
               var tool = clsCallTool();
               _mode = 3;                      
               tool.Conference(callid,conferencecallid);
               CustomEvent('',20007);
              
               return 0;                               
            },
          CancelConference: function()
            {                
                
               cancelconference();
               
               return 0;               
                               
            },
          SetCallStatus : function(statuscode,cid)
          {
            setbar(statuscode,cid);
            
            return 0;               
          },
          SetBtnHandle:function(btn,btntype)
            {
                switch (btntype)
                    {
                        case 1 : //Pickup
                            {
                                _btnpickup = btn;
                                try
                                {
                                    btn.onclick= this.Pickup; 
                                }
                                catch(e)
                                {
                                }
                                 
                                break;
                            }
                        case 2 : //Disconnect
                            {
                                _btndisconnect = btn;
                                try
                                {
                                    btn.onclick = this.Disconnect; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                        case 3 ://Hold
                            {
                                _btnhold = btn;
                                try
                                {
                                    btn.onclick = this.Hold; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                        case 4 ://Mute
                            {
                                _btnmute = btn;
                                try
                                {
                                    btn.onclick = this.Mute; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 5 ://Dail
                            {
                                _btndail = btn;
                                try
                                {
                                    btn.onclick = this.Dail; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 6 ://BlindTransfer
                            {
                                _btnblindtransfer = btn;
                                try
                                {
                                    btn.onclick = this.BlindTransfer; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 7 ://MakeTransfer
                            {
                                _btnmaketransfer = btn;
                                try
                                {
                                    btn.onclick = this.MakeTransfer; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 8 ://ConsultTransfer
                            {
                                _btnconsulttransfer = btn;
                                try
                                {
                                    btn.onclick = this.ConsultTransfer; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 9 ://CancelTransfer
                            {
                                _btncanceltransfer = btn;
                                try
                                {
                                    btn.onclick = this.CancelTransfer; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 10 ://MakeTransfer
                            {
                                _btnmakeconference = btn;
                                try
                                {
                                    btn.onclick = this.MakeConference; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 11 ://ConsultTransfer
                            {
                                _btnconsultconference = btn;
                                try
                                {
                                    btn.onclick = this.Conference; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }
                         case 12 ://CancelTransfer
                            {
                                _btncancelconference = btn;
                                try
                                {
                                    btn.onclick = this.CancelConference; 
                                }
                                catch(e)
                                {
                                }
                                break;
                            }

                    }
            },
            SetTxtPhoneNum:function(txtNum)
            {
                _txtNumber = txtNum;
            }
           
          
        };
    
}
